//
//  MineUpdatePasswordViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/23.
//

import UIKit

class MineUpdatePasswordViewController: BaseViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavi()
        
        setupUI()
    }
    private func setupNavi() {
        navigationItem.title = "修改密码"
    }
    private func setupUI() {
        [oldPasswordTextField, newPasswordTextField, new1PasswordTextField, confirmButton].forEach { subView in
            view.addSubview(subView)
        }
        
        oldPasswordTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(30)
            make.height.equalTo(60)
        }
        newPasswordTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(oldPasswordTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        new1PasswordTextField.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(newPasswordTextField.snp.bottom).offset(20)
            make.height.equalTo(60)
        }
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(new1PasswordTextField.snp.bottom).offset(48)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
        }
    }
    @objc private func confirmButtonAction() {
        let oldPW = oldPasswordTextField.text?.isEmpty ?? false
        let newPW = newPasswordTextField.text?.isEmpty ?? false
        let new1PW = new1PasswordTextField.text?.isEmpty ?? false
        
        if oldPW {
            Toast.with(message: "请输入密码").showOnWindow()
            return
        }
        if newPW {
            Toast.with(message: "请输入新密码").showOnWindow()
            return
        }
        if new1PW {
            Toast.with(message: "请再输入密码").showOnWindow()
            return
        }
        if let newPw = newPasswordTextField.text, let new1Pw = new1PasswordTextField.text, newPW != new1PW {
            Toast.with(message: "两次输入的密码不一致").showOnWindow()
            return
        }
        Toast.with(message: "修改成功").showOnWindow()
        navigationController?.popViewController(animated: true)
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var oldPasswordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var newPasswordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入新密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var new1PasswordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "确认新密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var confirmButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setTitle("确认修改", for: .normal)
        button.setTitle("确认修改", for: .selected)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        return button
    }()
}
